import cn.test.entity.User;
import cn.test.service.LoginService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.sql.Date;
import java.util.List;

/**
 * Created by sam on 2017/11/23.
 */
public class test {
    LoginService loginService=null;
    @Before
    public void init() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mvc.xml");
        loginService = ac.getBean("loginService", LoginService.class);
    }
    @Test
    public void test1() throws SQLException {
        /*
        * 启动容器
        *
        * */
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mvc.xml");
        /*
        * DataSource是一个接口
        * BasicDataSource是DataSource的实现类
        * */
        DataSource ds = ac.getBean("ds", DataSource.class);
        System.out.println(ds.getConnection());
    }
    @Test
    public void testUserPwd() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mvc.xml");
        LoginService loginService = ac.getBean("loginService", LoginService.class);
        User user = loginService.checkLogin("sam", "1234");
        System.out.println(user);
    }

    @Test
    public void testFindByAll() {
        List<User> users = loginService.findbyAll();
        System.out.println(users);
    }
    @Test
    public void testSave() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        User user = new User();
        user.setUsername("jack");
        user.setPassword("5678");
        user.setName("李德本");
        user.setBirthday(Date.valueOf("1988-05-06"));
        loginService.save(user);

    }

    @Test
    public void testUpdate() {
        User user= loginService.checkLogin("jack", "5678");
        user.setName("李德");
        loginService.updateById(user);
    }
}
